package com.swh.xbsc.controller;

import com.swh.xbsc.entity.Article;
import com.swh.xbsc.entity.Result;
import com.swh.xbsc.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.Map;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/11/17 20:49
 * @Version: 1.0
 */
@RestController
@RequestMapping("/mapper/article")
public class ArticleController {

    @Autowired
    ArticleService service;

    @RequestMapping("")
    public ModelAndView index() {
        return new ModelAndView("/article.html");
    }

    @RequestMapping("/collection")
    public ModelAndView collection() {
        return new ModelAndView("/article_detail.html");
    }

    @RequestMapping("/release")
    public ModelAndView release() {
        return new ModelAndView("/article_add.html");
    }


    @RequestMapping("selectPage/{pageNum}/{pageSize}")
    public Result selectPage(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize,
                             @RequestParam Map<String, Object> map) {
        return new Result(service.selectPage(pageNum, pageSize,map));
    }

    @RequestMapping("selectPage2/{pageNum}/{pageSize}")
    public Result selectPage2(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize,
                             @RequestParam Map<String, Object> map) {
        return new Result(service.selectPage2(pageNum, pageSize,map));
    }

    @RequestMapping(value = "doUpdate", method = RequestMethod.POST)
    public Result doUpdate(@RequestBody Article article) {
        return new Result(service.updateByPrimaryKeySelective(article));
    }

    @RequestMapping(value = "insert",method = RequestMethod.POST)
    public Result insert(@RequestBody Article article){
        article.setUserId(1);
        article.setPublishRealName("admin");
        article.setPublishDate(new Date());
        return new Result(service.insertSelective(article));
    }



}
